package com.vim.modules.web.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vim.common.base.Constants;
import com.vim.common.model.response.Response;
import com.vim.common.model.response.RestfulResponse;
import com.vim.common.model.response.TableListResponse;
import com.vim.common.utils.FTPUtil;
import com.vim.common.utils.IdGen;
import com.vim.common.utils.ValidateUtils;
import com.vim.modules.web.model.ShopGoods;
import com.vim.modules.web.service.ShopGoodsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.util.List;

@RestController
@RequestMapping("/shopGoods")
public class ShopGoodsController {

    @Value("${res.url}")
    private String resUrl;

	@Resource
	private ShopGoodsService shopGoodsService;

	/**
	 * 列表查询
     * @param page 页码
     * @param limit 每页显示数量
	 */
	@RequestMapping("/list")
    public TableListResponse list(ShopGoods shopGoods, @RequestParam(value = "page") Integer page, @RequestParam(value = "limit") Integer limit){
        ValidateUtils.checkNotZero(page);
        ValidateUtils.checkNotZero(limit);
	    PageHelper.startPage(page,limit);
        List<ShopGoods> list = shopGoodsService.findList(shopGoods);
        PageInfo<ShopGoods> pageInfo = new PageInfo<>(list);
        return new TableListResponse(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public RestfulResponse edit(ShopGoods shopGoods){
        if(StringUtils.isNoneBlank(shopGoods.getGoodsId())){
            shopGoodsService.update(shopGoods);
        }else{
            shopGoodsService.save(shopGoods);
        }
        return new RestfulResponse(Response.OK);
    }

    /**
     * 详情
     * @param id 主键ID
     */
    @RequestMapping("/detail")
    public RestfulResponse detail(@RequestParam(value = "id") String id){
        ValidateUtils.checkNotBlank(id);
        return new RestfulResponse(shopGoodsService.find(id));
    }

    /**
     * 删除
     * @param id 主键ID
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public RestfulResponse del(@RequestParam(value = "id") String id){
        ValidateUtils.checkNotBlank(id);
        shopGoodsService.delete(id);
        return new RestfulResponse(Response.OK);
    }

    /**
     * 上传图片
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public RestfulResponse upload(@RequestParam("image") MultipartFile image){
        try {
            String suffix = image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf("."));
            if(StringUtils.isBlank(suffix)){
                return new RestfulResponse(RestfulResponse.DEFAULT_ERROR, "文件错误");
            }
            if(!suffix.equals(".jpg") && !suffix.equals(".png")){
                return new RestfulResponse(RestfulResponse.DEFAULT_ERROR, "文件格式错误");
            }

            String url = Constants.FILE_PATH.GOODS_IMAGE+ IdGen.dateDirectory();
            String filename= IdGen.uuid() + suffix;
            FTPUtil ftpUtil = new FTPUtil();
            boolean flag = ftpUtil.uploadLocalFile(url, (FileInputStream) image.getInputStream(), filename);
            if(flag){
                return new RestfulResponse(resUrl + url+filename);
            }else{
                return new RestfulResponse(RestfulResponse.DEFAULT_ERROR, "上传失败");
            }
        }catch (Exception e){
            return new RestfulResponse(RestfulResponse.DEFAULT_ERROR, "上传失败");
        }
    }
}

